package com.example.myapplication;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

/*
登录成功和注册成功后跳转的界面
 */
public class DetailsActivity extends AppCompatActivity {
    DBOpenHelper dbOpenHelper;
    SQLiteDatabase db;
    private ListView listview;
    List<details>list;
    details details;
    ItemListviewAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        list=new ArrayList<>();
        dbOpenHelper=new DBOpenHelper(this,"users.db",null,2);
        db=dbOpenHelper.getWritableDatabase();
        initView();
        initData();
        adapter=new ItemListviewAdapter(this,list);
        listview.setAdapter(adapter);



    }
/*
初始化控件
 */
    private void initView() {
        listview = (ListView) findViewById(R.id.listview);
    }
    /*
    舒适化适配器数据
     */
    public void initData(){
        Cursor cursor=db.query("user",null,null,null,null,null,null);
        //通过判断字段数量，是否拥有sex字段,
        // 数量为4有sex字段，为3没有该字段
        if (cursor.getColumnCount()==4) {
            while (cursor.moveToNext()) {
                String username = cursor.getString(cursor.getColumnIndex("username"));
                String password = cursor.getString(cursor.getColumnIndex("password"));
                String sex = cursor.getString(cursor.getColumnIndex("sex"));
                details=new details(username,password,sex);
                Log.d("ccccc", "getColumnCount()==4: "+details);
                list.add(details);
            }
        }
        else if (cursor.getColumnCount()==3){
            while (cursor.moveToNext()) {
                String username = cursor.getString(cursor.getColumnIndex("username"));
                String password = cursor.getString(cursor.getColumnIndex("password"));
                details = new details(username, password, null);
                Log.d("ccccc", "getColumnCount()==3: "+details);
                list.add(details);
            }
        }
        cursor.close();
    }

}
